_realname=ghdl
pkgbase="mingw-w64-${_realname}"
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-${_realname}-llvm"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-mcode"
)
pkgver=5.0.1
pkgrel=3
pkgdesc='GHDL: the open-source analyzer, compiler, simulator and (experimental) synthesizer for VHDL'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://github.com/ghdl/ghdl'
license=('spdx:GPL-2.0-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-gcc-ada"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-autotools"
)
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
options=(!emptydirs)
source=("https://github.com/ghdl/ghdl/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-support-llvm-20.patch)
sha256sums=('a4cef27312a158c28498eeb62a470007354755b267adb62005a04b66143a1f92'
            '985963845c63b12907891580b08c4273c4178a111f35c950f77e10c9fa63da23')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-support-llvm-20.patch
}

build() {
  echo "Building ghdl-llvm..."
  mkdir -p "${srcdir}"/build-${MSYSTEM}-llvm && cd "${srcdir}"/build-${MSYSTEM}-llvm

  CC=clang \
  CXX=clang++ \
  MSYS2_ARG_CONV_EXCL="--prefix" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-checks \
    --enable-libghdl \
    --enable-synth \
    --with-llvm-config

  make GNATMAKE="gnatmake -j$(nproc)"

  echo "Building ghdl-mcode..."
  mkdir -p "${srcdir}"/build-${MSYSTEM}-mcode && cd "${srcdir}"/build-${MSYSTEM}-mcode

  MSYS2_ARG_CONV_EXCL="--prefix" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-checks \
    --enable-libghdl \
    --enable-synth

  make GNATMAKE="gnatmake -j$(nproc)"
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-llvm"
  make install.vpi.local
  make test
}

package_ghdl-llvm() {
  pkgdesc+=" (LLVM backend) (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-llvm-libs")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  cd "${srcdir}"/build-${MSYSTEM}-llvm
  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/libghdl.link
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/ghdl/grt.lst

  # License
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/doc/licenses.rst "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-llvm/licenses.rst
}

package_ghdl-mcode() {
  pkgdesc+=" (mcode backend) (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-zlib")
  conflicts=("${MINGW_PACKAGE_PREFIX}-ghdl-llvm")
  cd "${srcdir}"/build-${MSYSTEM}-mcode
  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/libghdl.link

  # License
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/doc/licenses.rst "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-mcode/licenses.rst
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
